package com.yf.controller;

import com.yf.dto.Message;
import com.yf.dto.Page;
import com.yf.dto.Result;
import com.yf.entity.Role;
import com.yf.myException.ExceptionEnum;
import com.yf.myException.NewException;
import com.yf.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@Controller
@ResponseBody
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 增加角色
     * @param role
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/add_role",method = RequestMethod.POST)
    public Result addRole(Role role){
        List<List> list = new ArrayList<>();
        System.out.println(role);
        try {
            roleService.addRole(role);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_ADD);
        }
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/delete_role",method = RequestMethod.POST)
    public Result deleteRole(Integer roleId){
        System.out.println("执行删除");
        try {
            roleService.deleteRole(roleId);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_DELETE);
        }
    }

    /**
     * 更新角色
     * @param role
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/update_role")
    public Result updateRole(Role role){
        System.out.println("执行更新");
        try {
            roleService.updateRole(role);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_UPDATE);
        }
    }

    /**
     * 根据ID查询角色信息
     * @param roleId
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/getRoleByRoleId")
    public Result getRoleByRoleId(Integer roleId){
        System.out.println("执行查询角色信息根据ID");
        try {
            Role role = roleService.getRoleByRoleId(roleId);
            Result result = new Result();
            result.setMessage(new Message());
            result.setObj(role);
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_GET);
        }
    }

    /**
     * 查询分页信息
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/getRolePage")
    public Result getRolePage(Integer pageNo,Integer pageSize){
        System.out.println("执行查询分页列表");
        try {
            Page rolePage = roleService.getRolePage(pageNo, pageSize);
            Result result = new Result();
            result.setMessage(new Message());
            result.setObj(rolePage);
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_GET);
        }
    }



}
